from django.db import models
from django.contrib.auth.models import User
from django.newforms import ModelForm


# Create your models here.
 
class Tag(models.Model):
	slug = models.SlugField(
		'Slug',
		prepopulate_from=("title",),
		help_text='Automatically built from the title.',
		primary_key='True',
        	editable = False,
	)
    	creator = models.ForeignKey(User)
	title = models.CharField('Title', max_length=30)
	description = models.TextField(
		'Description',
		help_text='Short summary of this tag',
		blank=True
	)
	def __str__(self):
		return self.title
	def get_absolute_url(self):
		return "/blog/tag/%s/" % self.slug
	class Admin:
		list_display = ('slug', 'title',)
		search_fields = ('title', 'description',)
 
class Post(models.Model):
	slug = models.SlugField(
		'Slug',
		prepopulate_from=('title',),
		help_text='Automatically built from the title.',
        	editable=False,
	)
	author = models.ForeignKey(User)
	assoc_tags = models.ManyToManyField(Tag, blank=True)
	title = models.CharField('Title', max_length=30)
        is_public = models.BooleanField(default = False)
	create_date = models.DateTimeField('Create Date', auto_now_add=True)
	body = models.TextField('Body Text')
	def __str__(self):
		return self.title
	def get_absolute_url(self):
		return "/blog/%s/%s/" % (self.create_date.strftime("%Y/%b/%d").lower(), self.slug)
	class Admin:
		list_display = ('slug', 'title', 'create_date')
		search_fields = ('title', 'description')
        list_filter = ('author', 'create_date')
        date_hierarchy = 'create_date'
	class Meta:
		ordering = ('-create_date',)
        
        
class PostForm(ModelForm):
    class Meta:
        model = Post
        exclude = ('create_date', 'author',)
        
class TagForm(ModelForm):
    class Meta:
        model = Tag
        exclude = ('creator',)
        